<?php
namespace App\Api\Controllers\Zgjl;

use App\Api\Controllers\Zgjl\BaseController as base;

class PayController extends BaseController
{
    public function __construct()
    {

    }


    public function scan_pay($data)
    {
        try {
//            if ($data['ways_source'] == 'weixin') {
//                $txnType = '1006';
//                $txnSubType = '100608';
//                $txnAccType = '02';
//            }
//            if ($data['ways_source'] == 'alipay') {
//                $txnType = '1007';
//                $txnSubType = '100701';
//                $txnAccType = '03';
//            }
//            if ($data['ways_source'] == 'unionpay') {
//                $txnType = '1027';
//                $txnSubType = '102701';
//                $txnAccType = '04';
//            }
//
//            $params = [
//                'txnType' => $txnType,
//                'txnSubType' => $txnSubType,
//                'merId' => $data['mch_id'],
//                'aesWay' => '04',
//                'txnOrderId' => $data['out_trade_no'],
//                'txnOrderTime' => $this->micTime(),
//                'txnAccType' => $txnAccType,
//                'txnAuthCode' => $data['code'],
//                'txnAmt' => (string)($data['total_amount'] * 100),
//                'txnCcyType' => '156',
//                'terminalId' => $data['term_no'],
//                'terminalType' => '11'
//            ];

            $params = [
                'posNo' => $data['pos_no'],
                'key' => $data['key'],
                'tranType' => 'C',
                'scanCode' => $data['code'],
                'mid' => $data['mch_id'],
                'txnAmt' => (string)($data['total_amount'] * 100),
                'merTradeNo' => $data['out_trade_no'],
                'tid' => $data['pos_no'],
                'orderDesc' => '扫一扫',
                'retUrl' => url('/api/zgjl/pay_notify_url'),
            ];

//            if (isset($data['body']) && !empty($data['body'])) {
//                $params['txnOrderBody'] = $data['body'];
//            }

            $base = base::getInstance();
            $ret  = $base->params($params)->build('scan_pay', $base::SCAN_PAY_URL);

            if (isset($ret['respCode']) && !empty($ret['respCode'])) {
                if ($ret['respCode'] == "00") { //支付成功
                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret
                    ];
                } else if ($ret['respCode'] == '88') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getLine(),
            ];
        }
    }


    //查询订单 0-系统错误 1-成功 2-正在支付 3-失败 4.已经退款
    public function order_query($data)
    {
        try {
            $params = [
                'posNo' => $data['pos_no'],
                'key' => $data['key'],
                'tranType' => 'G',
                'mid' => $data['mch_id'],
                'merTradeNo' => $data['out_trade_no'],
                'tid' => $data['pos_no'],
            ];
            $base = base::getInstance();

            $url = $base::SCAN_PAY_URL;
            if (strpos($data['out_trade_no'], "qr") !== false) {
                $url = $base::QR_URL;
            }
            $ret  = $base->params($params)->build('order_query', $url);

            if (isset($ret['respCode']) && !empty($ret['respCode'])) {

                if ($ret['respCode'] == "00") { //支付成功
                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret
                    ];
                } else if ($ret['respCode'] == '88') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getLine(),
            ];
        }
    }


    //退款 0-系统错误 1-成功
    public function refund($data)
    {
        try {
            $params = [
                'posNo' => $data['pos_no'],
                'key' => $data['key'],
                'tranType' => 'R',
                'mid' => $data['mch_id'],
                'vfTradeNo' => $data['refund_no'],
                'merTradeNo' => $data['out_trade_no'],
                'txnAmt' => (string)($data['refund_amount'] * 100),
                'tid' => $data['pos_no'],
            ];

            $base = base::getInstance();

            $url = $base::SCAN_PAY_URL;
            if (strpos($data['out_trade_no'], "qr") !== false) {
                $url = $base::QR_URL;
            }

            $ret  = $base->params($params)->build( 'refund', $url);

            if (isset($ret['respCode']) && !empty($ret['respCode'])) {

                if ($ret['respCode'] == '0000') {
                    return [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => [
                            'refund_count' => 1,
                            'out_trade_no'  => $data['out_trade_no'],
                        ]
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 2,
                        'message' => $message
                    ];
                }
            } else {
                $message = '失败了';
                return [
                    'status' => 2,
                    'message' => $message
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 0,
                'message' => $ex->getMessage().' | '.$ex->getLine()
            ];
        }
    }


    //退款查询 0-系统错误 1-成功 2-正在退款 3-失败
    public function refund_query($data)
    {
        try {


        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }

    public function qr_submit($data)
    {
        try {
            $params = [
                'posNo' => $data['pos_no'],
                'key' => $data['key'],
                'tranType' => 'F',
                'mid' => $data['mch_id'],
                'txnAmt' => (string)($data['total_amount'] * 100),
                'merTradeNo' => $data['out_trade_no'],
                'tid' => $data['pos_no'],
                'orderDesc' => '扫一扫',
                'retUrl' => url('/api/zgjl/pay_notify_url'),
            ];

            $base = base::getInstance();
            $ret  = $base->params($params)->build('qr_submit', $base::QR_URL);

            if (isset($ret['respCode']) && !empty($ret['respCode'])) {
                if ($ret['respCode'] == '00') {
                    return [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => [
                            'codeUrl' => $ret['scanCode']
                        ]
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 2,
                        'message' => $message
                    ];
                }
            }
            return [
                'status' => 2,
                'message' => '接口异常'
            ];
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '. $ex->getLine(),
            ];
        }
    }

    public function terminal ($data)
    {
        try {
            $params = [
                'txnType' => '1000',
                'txnSubType' => '100015',
                'merId' => $data['mch_id'],
                'aesWay' => '04',
                'merName' => $data['merName'],
                'channelType' => $data['channelType'],
                'operationId' => $data['operationId'],
                'terminalType' => '11',
                'terminalState' => $data['terminalState'],
                'terminalAddress' => $data['terminalAddress'],
                'terminalId' => $data['terminalId'],
                'chnlType' => '01'
            ];

            $base = base::getInstance();
            $ret  = $base->params($params)->build('terminal');

            if (isset($ret['rspStatus']) && !empty($ret['rspStatus'])) {
                //PayResult 0成功，非0失败
                if ($ret['respCode'] == "000000") { //支付成功
                    return [
                        'status' => 1,
                        'message' => '成功',
                        'data' => $ret
                    ];
                }  else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getLine(),
            ];
        }
    }





}
